package com.blog4j.contentVerify.utils;

import com.blog4j.contentVerify.exception.ContentVerifyException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 98k灬
 * @version v1.0.0
 * @Description : 功能描述
 * @Create on : 2024/9/6 12:30
 **/
@Slf4j
public class ContentVerifyUtil {
    /**
     * 将字符串按照指定的长度分割
     *
     * @param length 指定的长度
     * @param sliceStr 带分割的字符串
     * @return 分割后的字符串组成的数组
     */
    public static List<String> sliceStrByLength(int length, String sliceStr) {
        if (length < 0) {
            throw new ContentVerifyException("切割字符串的长度不能小于0");
        }

        if (StringUtils.isBlank(sliceStr)) {
            throw new ContentVerifyException("切割字符串不能为空");
        }

        List<String> list = new ArrayList<>();
        try {
            int start = 0;
            int sliceStrLength = sliceStr.length();
            while(start < sliceStrLength) {
                int end = Math.min(sliceStrLength, start + length);
                String subStr = sliceStr.substring(start, end);
                start += length;
                list.add(subStr);
            }
        } catch (Exception exception) {
            log.error("字符串分割出现异常, [{}]", exception.getMessage());
            throw new ContentVerifyException("字符串分割出现异常");
        }
        return list;
    }
}
